package com.antu.first.service.impl;

import com.antu.first.bean.Merchant;
import com.antu.first.dao.MerchantLoginDao;
import com.antu.first.service.MerchantLogionService;
import com.antu.first.util.CodeUtil;
import com.antu.first.util.MD5;
import com.antu.first.util.PhoneCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@Service("merchantLogionService")
public class MerchantLoginServiceImpl implements MerchantLogionService {
    @Autowired
    private MerchantLoginDao merchantLoginDao;
    private Merchant merchant;
    @Autowired
    private MerchantLogionService merchantLogionService;
    private int count=0;
    Map<String,Object> map = new HashMap<String,Object>();
    @Override
    public Map<String, Object> merchantlogin(Merchant merchant, HttpServletRequest request) throws Exception{
        String username = merchant.getmUsername();
        String password = merchant.getmPassword();
        String md5Pass=MD5.getMd5(password);
        Merchant merchant1=new Merchant();
        merchant1.setmUsername(username);
        merchant1.setmPassword(md5Pass);
        /*1表账号为空 2表密码为空 3表账号已经进行了登录，不能再进行登录 4表登录成功
        5表验证码错误 6表服务器繁忙 7表账号已经进行了登录，不能再进行登录 8表登录成功*/
        if (username != null && !"".equals(username)) {
            if (password != null && !"".equals(password)) {
                //在数据库密码用md5的时候，这里对象改成merchant1-------------------等改
                    Merchant m= merchantLoginDao.merchantLogion(merchant);
                    if (m != null) {
                        if(count<3){
                            /*if (m.isOnline() == true) {
                            count+=1;
                            map.put("success",false);
                            map.put("errMsg", "账号已经进行了登录，不能再进行登录");
                            } else {*/
                                map.put("success",true);
                                //map.put("success","登录成功!");
                                request.getSession().setAttribute("merchant", m);
                                /*int i=merchantLogionService.updateMerchantIsOnline(m);
                                if(i>0){
                                    //System.out.println("商家在线状态修改成功");
                                    map.put("isOnline","商家在线状态修改成功");
                                }else {
                                    //System.out.println("商家在线状态修改失败");
                                    map.put("isOnline","商家在线状态修改失败");
                                }*/
                           // }
                        }else{
                            if (CodeUtil.checkVerifyCode(request)) {
                                /*if (m.isOnline() == true) {
                                    count+=1;
                                    map.put("success",false);
                                    map.put("errMsg", "账号已经进行了登录，不能再进行登录");
                                } else {*/
                                    map.put("success",true);
                                    //map.put("success", "登录成功");
                                    request.getSession().setAttribute("merchant", merchant);
                                    /*int i=merchantLogionService.updateMerchantIsOnline(m);
                                    if(i>0){
                                        map.put("isOnline","商家在线状态修改成功");
                                    }else {
                                        map.put("isOnline","商家在线状态修改失败");
                                    }*/
                                //}
                            } else {
                                count+=1;
                                map.put("success",false);
                                map.put("errMsg", "验证码错误");
                            }
                        }
                    } else {
                        count+=1;
                        map.put("success",false);
                        map.put("errMsg", "账号或者密码错误");
                    }
            } else {
                count+=1;
                map.put("success",false);
                map.put("errMsg", "密码为空");
            }
        }else{
            count+=1;
            map.put("success",false);
            map.put("errMsg", "账号为空");
        }
        return map;
    }

    @Override
        public Map<String,Object> merchantForgetPassword(String mNewPassword,String mCode,HttpServletRequest request) throws Exception{
            String merchantPhone = (String) request.getSession().getAttribute("merchantPhone");
            String merchantPhoneCode = (String) request.getSession().getAttribute("merchantPhoneCode");
            String reg = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";//必须同时包含数字和字母，且长度要在6-16位之间。
            Merchant merchant=new Merchant();
            if(mCode!=null&&!"".equals(mCode)){
               if(mNewPassword!=null&&!"".equals(mNewPassword)){
                    if (mCode.equals(merchantPhoneCode)) {
                        if (mNewPassword.matches(reg)) {
                            merchant.setmPhone(merchantPhone);
                            merchant.setmPassword(mNewPassword);
                            int n = merchantLoginDao.merchantForgetPassword(merchant);
                            if (n > 0) {
                                map.put("success",true);
                                //map.put("success", "密码重置成功");
                            } else {
                                map.put("success",false);
                                map.put("errMsg", "修改失败，请核对信息是否正确");
                            }
                        } else {
                            map.put("success",false);
                            map.put("errMsg", "密码格式错误");
                        }
                    } else {
                        map.put("success",false);
                        map.put("errMsg", "手机验证码错误");
                    }
            }else {
                map.put("success",false);
                map.put("errMsg", "密码不能为空");
            }
           }else {
               map.put("success",false);
               map.put("errMsg", "验证码不能为空");
           }
            return map;
        }
        @Override
        public Map<String, Object> validateMerchantPhone(String mPhone,HttpServletRequest request) throws Exception{
            if (mPhone != null && !"".equals(mPhone)) {
                if (mPhone.length() == 11) {
                    Merchant merchant=merchantLogionService.selectMerchantByPhone(mPhone);
                    if(merchant!=null){
                        PhoneCode phoneCode = new PhoneCode();
                        String merchantPhoneCode = phoneCode.code(mPhone);
                        request.getSession().setAttribute("merchantPhoneCode", merchantPhoneCode);
                        request.getSession().setAttribute("merchantPhone", mPhone);
                        map.put("success",true);
                    }else {
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("success",false);
                        map.put("errMsg","手机号码不存在，请核对");
                        return map;
                    }
                } else {
                    map.put("success",false);
                    map.put("errMsg", "手机号不是11位");
                }
            } else {
                map.put("success",false);
                map.put("errMsg", "手机号为空");
            }
            return map;
        }
    //当商家登录时修改在线状态
    @Override
    public int updateMerchantIsOnline(Merchant merchant) throws Exception{
        int n=merchantLoginDao.updateMerchantIsOnline(merchant);
        return n;
    }
    //商家退出后修改在线状态
    @Override
    public Map<String,Object> updateMerchantIsOnlineAfterExit(Merchant merchant){
        int n=merchantLoginDao.updateMerchantIsOnlineAfterExit(merchant);
        if(n>0){
            map.put("success", true);
        }else {
            map.put("errMsg", "商家退出后修改在线状态失败");
        }
        return map;
    }

    @Override
    public Merchant selectMerchantByPhone(String mPhone){
        return  merchantLoginDao.selectMerchantByPhone(mPhone);
    }
}